package com.ruoyi.web.controller;

import com.aliyun.oss.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.result.Result;
import com.ruoyi.system.domain.Oss;
import com.ruoyi.system.service.IOssService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static com.ruoyi.common.result.ResultCodeEnum.ARGUMENT_VALID_ERROR;
import static org.apache.naming.SelectorContext.prefix;

/**
 * <p>
 * 资源表 前端控制器
 * </p>
 *
 * @author FSD
 * @since 2024-02-18
 */
@Api(tags = "资源表")
@RestController
@RequestMapping("/api/system/oss")
public class OssController {
    private String prefix = "/api/system/oss";
    @Resource(name = "OssServiceImpl")
    private IOssService ossService;


    @RequiresPermissions("system:oss:view")
    @GetMapping()
    public String oss()
    {
        return prefix + "/oss";
    }
    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增", notes = "新增")
    @ApiImplicitParam(name = "oss", value = "资源实体类",dataType = "String",dataTypeClass = String.class)
    @RequiresRoles(value = {"admin", "teacher"}, logical = Logical.OR)
    public Result insert(@RequestBody Oss oss) {
        return Result.ok(ossService.save(oss));
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改", notes = "修改")
    @ApiImplicitParam(name = "oss", value = "资源实体类",dataType = "String",dataTypeClass = String.class)
    @RequiresRoles(value = {"admin", "teacher"}, logical = Logical.OR)
    public Result update(@RequestBody Oss oss) {
        return Result.ok(ossService.updateById(oss));
    }

    /**
     * 根据id删除
     */
    @PostMapping("/delete/{id}")
    @ApiOperation(value = "根据id删除", notes = "根据id删除")
    @ApiImplicitParam(name = "id", value = "id",dataType = "Integer",dataTypeClass = Integer.class)
    @RequiresRoles(value = {"admin", "teacher"}, logical = Logical.OR)
    public Result delete(@PathVariable Integer id) {
        return Result.ok(ossService.removeById(id));
    }

    /**
     * 上传图片
     */
    @PostMapping("upload")
    @ApiOperation(value = "上传图片", notes = "上传图片")
    @ApiImplicitParam(name = "file", value = "文件",dataType = "File",dataTypeClass = String.class)
    @RequiresRoles(value = {"admin", "teacher"}, logical = Logical.OR)
    public Result<String> upload(@RequestParam("file") MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            return Result.fail("传参为空", ARGUMENT_VALID_ERROR);
        } else {
            return ossService.upload(file);
        }
    }

    /**
     * 下载图片
     */
    @PostMapping("download")
    @ApiOperation(value = "下载图片", notes = "下载图片")
    @ApiImplicitParam(name = "urlString", value = "页面穿过来的文件名字",dataType = "String",dataTypeClass = String.class)
    @RequiresRoles(value = {"admin", "teacher"}, logical = Logical.OR)
    public Result<String> download(HttpServletRequest request, HttpServletResponse response,
                                   @RequestParam String urlString) throws IOException {
        if (StringUtils.isNullOrEmpty(urlString)) {
            return Result.fail("传参为空", ARGUMENT_VALID_ERROR);
        }
        return ossService.download(request, response, urlString);
    }

    /**
     * 查询全部
     */
    @GetMapping("/getAll")
    @ApiOperation(value = "查询全部", notes = "查询全部")
    @RequiresRoles(value = {"admin", "teacher"}, logical = Logical.OR)
    public Result findAll() {
        return Result.ok(ossService.list());
    }

    /**
     * 根据id查询一个
     */
    @GetMapping("getById/{id}")
    @ApiOperation(value = "根据id查询一个", notes = "根据id查询一个")
    @ApiImplicitParam(name = "id", value = "id",dataType = "Integer",dataTypeClass = Integer.class)
    @RequiresRoles(value = {"admin", "teacher"}, logical = Logical.OR)
    public Result findOne(@PathVariable Integer id) {
        return Result.ok(ossService.getById(id));
    }

    /**
     * 分页模糊查询
     */
    @GetMapping("/get/page")
    @ApiOperation(value = "分页模糊查询", notes = "分页模糊查询")
    @RequiresRoles(value = {"admin", "teacher"}, logical = Logical.OR)
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<Oss> queryWrapper = new QueryWrapper<>();
        // 倒序查询
        queryWrapper.orderByDesc("id");
        return Result.ok(ossService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }
}

